<?php
/**
* @package Joomla.Tutorials
* @subpackage Components
* @link http://dev.joomla.org/component/option,com_jdwiki/
Itemid,31/id,tutorials:modules/
* @license GNU/GPL
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
/**
* Hello World Component Controller
*
* @package Joomla.Tutorials
* @subpackage Components
*/
class AccessrulesController extends JController
{

	/**
	* constructor (registers additional tasks to methods)
	* @return void
	*/
	
	function __construct() 	{
		parent::__construct();
		// Register Extra tasks
		$this->registerTask( 'add' , 'edit' );
	}
	
	/**
	* Method to display the view
	*
	* @access public
	*/
	function display() {
		parent::display();
	}
	
	
	/**
	* display the edit form
	* @return void
	*/
	function edit()	{
		JRequest::setVar( 'view', 'hr' );//lay thu muc: administrator\components\com_hello\views\hello\ (giong cau thuc thu muc cua ...\views\hellos\
		JRequest::setVar( 'layout', 'form' ); //"form": lay trong thu muc views\hellos\tmpl\form.php
		JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}
	
	/**
	* save a record (and redirect to main page)
	* @return void
	*/
	function save()	{
		$model = $this->getModel('hello');
		if ($model->store()) {
			$msg = JText::_( 'Greeting Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Greeting' );
		}
		// Check the table in so it can be edited.... we are done with it	anyway
		$link = 'index.php?option=com_hello';
		$this->setRedirect($link, $msg);
	}
	
	/**
	* save a record (and redirect to main page)
	* @return void
	*/
	//function searchContents()	{
		/*$model = $this->getModel('hello');
		if ($model->store()) {
			$msg = JText::_( 'Greeting Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Greeting' );
		}
		// Check the table in so it can be edited.... we are done with it	anyway*/
		//JRequest::setVar( 'view', 'accessrules' );
		//parent::display();
		//JRequest::setVar( 'layout', 'form' )
		
		//$link = 'index.php?option=com_accessrule';
		//$this->setRedirect($link, "TUYEN");
	//}
	
	/**
	* remove record(s)
	* @return void
	*/
	function remove() {
		$model = $this->getModel('hello');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More Greetings Could not be Deleted' );
		} else {
			$msg = JText::_( 'Greeting(s) Deleted' );
		}
		$this->setRedirect( 'index.php?option=com_hello', $msg );
	}
	
	/**
	* cancel editing a record
	* @return void
	*/
	function cancel() {
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_hello', $msg );
	}
	
	
	
	function searchUserType() {  
		global $mainframe;
		$model = $this->getModel('Accessrules');
		
		$userTypeName= JRequest::getVar( 'userTypeName', -1, 'get', 'string' );
		
		//echo (JRequest::getVar( 'userTypeName', -1, 'post', 'string' ));
		
		if (!strcmp (strtolower($userTypeName), strtolower('User'))) {
			$__data = $model->getUsers();
		} else if (!strcmp (strtolower($userTypeName), strtolower('Group'))) {
			$__data = $model->getGroups();
		} else {
			$return = "Hay chon nhom USER";
		}
		
		$i = 0;
		if (!empty($__data)) {
			foreach ($__data as $row) {
				echo '<input type="checkbox" name="userTypeCheckbox" id="idUserTypeCheckbox'.$i.'" onclick ="" value="'.$row->id.'" />'.$row->name.'<br/>';
				$i++;
			}
		}
		echo $return;
		$mainframe->close();
	}
	
	function searchContents() {  
		global $mainframe;
		$model = $this->getModel('Accessrules');
		
		//$userTypeName= JRequest::getVar( 'userTypeName', -1, 'get', 'string' );
		
		$typeofSerachContent= JRequest::getVar( 'typeofSerachContent', -1, 'get', 'string' );
		
		$__idSections  = JRequest::getVar( 'idSections', -1, 'get', 'int' );
		$__idCategories = JRequest::getVar( 'idCategories', -1, 'get', 'int' );

		if (!strcmp (strtolower($typeofSerachContent), strtolower('CONTENT'))) {
			$__data = $model->getContentsBySecAndCat ($__idSections, $__idCategories);
		} else if (!strcmp (strtolower($typeofSerachContent), strtolower('CATEGORY'))) {
			$__data = $model->getCategories ($__idSections);
		}
		$i = 0;
	    $return = "";
		if (!empty($__data)) {
			foreach ($__data as $row) {
				$return =  $return.'<input type="checkbox" name="contentCheckbox" id="idcontentCheckbox'.$i.'" onclick ="" value="'.$row->id.'" />'.'&nbsp;';
				$return = $return.$row->title;
				$return = $return.'<br/>';
				$i++;
			}
		}
		echo $return.'&nbsp;';//prevent blank case
		$mainframe->close();
	}
	
	function saveAll() {  
		global $mainframe;
		$model = $this->getModel('Accessrules');

		$__request_arrUserTypeCheckbox = JRequest::getVar( 'arrUserTypeCheckbox', array(0), 'get', 'array' );//Display from Request "1,2,3,4"
		$arrUserTypeCheckbox = explode(",", $__request_arrUserTypeCheckbox[0] );//Remove "," and insert elements into array
		
		$__request_arrContentsCheckbox = JRequest::getVar( 'arrContentsCheckbox', array(0), 'get', 'array' );//Display from Request "1,2,3,4"
		$arrContentsCheckbox = explode(",", $__request_arrContentsCheckbox[0] );//Remove "," and insert elements into array
		
//		$userTypeName= JRequest::getVar( 'userTypeName', -1, 'get', 'string' );
		
		$return = $model->saveAll ($arrUserTypeCheckbox, $arrContentsCheckbox, JRequest::getVar( 'userTypeName', -1, 'get', 'string' ), JRequest::getVar( 'contentTypeName', -1, 'get', 'string' ), JRequest::getVar( 'rules', -1, 'get', 'string' ));
		echo $return.'&nbsp;';      
		$mainframe->close();
	}
}
?>